﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WStest.Models
{
    
    public class Service
    {
        ServiceReference1.ToWaWebServiceClient ws = new ServiceReference1.ToWaWebServiceClient();

        public int AddChatMessage(long time, string message, int userId)
        {
            return ws.addChatMessage(time, message, userId);
        }

        public List<UserView> FindUsers(string username)
        {
            List<UserView> list = new List<UserView>();
            ServiceReference1.userView[] uv = ws.findUsers(username);
            if (uv != null)
            {
                foreach (ServiceReference1.userView u in uv)
                {
                    list.Add(getUserView(u));
                }
            }
            return list;
        }

        public List<UserView> GetFriendsByUserId(int id)
        {
            List<UserView> list = new List<UserView>();
            ServiceReference1.userView[] uv = ws.getFriendsByUserId(id);
            if (uv != null)
            {
                foreach (ServiceReference1.userView u in uv)
                {
                    list.Add(getUserView(u));
                }
            }
            return list;
        }

        public List<PostitView> GetLatestPostitsByUserId(int userId)
        {
            List<PostitView> list = new List<PostitView>();
            ServiceReference1.postitView[] pv =  ws.getLatestPostitsByUserId(userId);
            if (pv != null)
            {
                foreach (ServiceReference1.postitView p in pv)
                {
                    list.Add(getPostitView(p));
                }
            }
            return list;
        }

        public List<ChatView> GetMessageAfter(int id, long start)
        {
            List<ChatView> list = new List<ChatView>();
            ServiceReference1.chatView[] cv = ws.getMessageAfter(338, start);
            if (cv != null)
            {
                foreach (ServiceReference1.chatView c in cv)
                {
                    list.Add(getChatView(c));
                }
            }
            return list;
        }

        public PrivatemessageView GetPrivateMessageById(int id)
        {
            return getPrivateMessageView(ws.getPrivateMEssageById(id));
        }

        public List<PrivatemessageView> GetPrivateMessagesReceivedByUserId(int userId)
        {
            List<PrivatemessageView> list = new List<PrivatemessageView>();
            ServiceReference1.privatemessageView[] pmv = ws.getPrivateMessagesReceivedByUserId(userId);
            if (pmv != null)
            {
                foreach (ServiceReference1.privatemessageView p in pmv)
                {
                    list.Add(getPrivateMessageView(p));
                }
            }
            return list;
        }

        public List<PrivatemessageView> GetPrivateMessagesSentByUserId(int userId)
        {
            List<PrivatemessageView> list = new List<PrivatemessageView>();
            ServiceReference1.privatemessageView[] pmv = ws.getPrivateMessagesSentByUserId(userId);
            if (pmv != null)
            {
                foreach (ServiceReference1.privatemessageView p in pmv)
                {
                    list.Add(getPrivateMessageView(p));
                }
            }
            return list;
        }

        public UserView GetUserById(int id)
        {
            return getUserView(ws.getUserById(id));
        }

        public UserView GetUserByUsername(string username)
        {
            return getUserView(ws.getUserByUsername(username));
        }

        public bool MakeFriend(int userId, int friendId)
        {
            string s = ws.makeFriend(userId, friendId);
            if (s.Equals("befriended"))
                return true;
            return false;
        }

        public bool RemoveFriend(int userId, int friendId)
        {
            string s = ws.removeFriend(userId, friendId);
            if (s.Equals("unfriended"))
                return true;
            return false;
        }

        public bool Stalkable(int userId, int friendId)
        {
           string s = ws.stalkable(userId, friendId);
           if (s.Equals("befriendable"))
               return true;
           return false;
        }

        public bool SubmitPostIt(int userId, string content)
        {
            string s = ws.submitPostIt(userId, content);
            if(s.Equals("postitAdded"))
                return true;
            return false;
        }

        public bool SubmitPrivateMessage(int userId, string targetName, string title, string content)
        {
           return ws.submitPrivateMessage(userId, targetName, title, content);
        }

        public bool UpdatePassword(int id, string oldPassword, string password, string confirmPassword)
        {
            return ws.updatePassword(id, oldPassword, password, confirmPassword);
        }

        public bool updateProfile(int id, string firstName, string surName, string address, string zipcode, string city, string country)
        {
           return ws.updateProfile(id, firstName, surName, address, zipcode, city, country);
        }

        private ChatView getChatView(ServiceReference1.chatView cv)
        {
            ChatView view = new ChatView();
            view.content = cv.content;
            view.date = cv.date;
            view.Id = cv.id;
            view.username = cv.username;
            return view;
        }

        private PostitView getPostitView(ServiceReference1.postitView pv)
        {
            PostitView view = new PostitView();
            view.content = pv.content;
            view.date = pv.date;
            view.username = pv.username;
            return view;
        }

        private PrivatemessageView getPrivateMessageView(ServiceReference1.privatemessageView pmv)
        {
            PrivatemessageView view = new PrivatemessageView();
            view.Content = pmv.content;
            view.Date = pmv.date;
            view.FromUser = pmv.fromUser;
            view.Id = pmv.id;
            view.Title = pmv.title;
            view.ToUser = pmv.toUser;
            return view;
        }

        private UserView getUserView(ServiceReference1.userView uv)
        {
            UserView view = new UserView();
            view.Address = uv.address;
            view.City = uv.city;
            view.Country = uv.country;
            view.Email = uv.email;
            view.FirstName = uv.firstName;
            view.Id = uv.id;
            view.SurName = uv.surName;
            view.Username = uv.username;
            view.Zipcode = uv.zipcode;
            return view;

        }

        public long GetUnixEpoch2(DateTime dateTime)
        {
            var unixTime = dateTime.ToUniversalTime() -
                new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

            return (long) unixTime.TotalSeconds;
        }
    }
}